<?php
if($_SERVER['REMOTE_ADDR'] != "10.0.42.71" and $_SERVER['REMOTE_ADDR'] != "199.77.34.238"){
	exit;
}
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.inc.php');
include_once('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edison Technology Employee TimeClock</title>
<link rel="stylesheet" type="text/css" href="css/style2.css" /> 
<link rel="stylesheet" type="text/css" href="css/jquery.jdigiclock.css" /> 
<script type="text/javascript" src="lib/jquery-1.3.2.min.js"></script> 
<script type="text/javascript" src="lib/jquery.jdigiclock.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() {
 	$('#digiclock').jdigiclock({
		announcement: 'No Announcements Currently'
	});
});
</script> 
</head>

<body>
<div id="digiclock"></div>
<?php
if($_POST['bannerid'] != "00039064"){
	echo "<meta http-equiv='refresh' content='15;URL=timecard.php'>";
}
if(isset($_POST['action'])){
	$sql = "SELECT * FROM SPRIDEN, GOBTPAC WHERE SPRIDEN_PIDM = GOBTPAC_PIDM AND SPRIDEN_ID = :banid AND SPRIDEN_CHANGE_IND is 	null";
	$q2 = Oracle::Query($sql, array(':banid'=>"@".$_POST['bannerid']));
	$row2 = $q2->fetch();
	
	$sql = "SELECT * FROM `tbEmployees` WHERE `sFname` = :fname AND `sLname` = :lname";
	$q = db::Query($sql, array(':fname'=>$row2['SPRIDEN_FIRST_NAME'], ':lname'=>$row2['SPRIDEN_LAST_NAME']));
	$row = $q->fetch();
	if($row['iID'] == NULL){
		echo "<meta http-equiv='refresh' content='0;URL=timecard.php?msg=You are currently not active in the Portal System please contact Kody'>";
		exit;
	}
		
	$sql = "SELECT * FROM `tbTimecard` WHERE `iEmployeeID` = :empid ORDER BY `iID` DESC";
	$q = db::Query($sql, array(':empid'=>$row['iID']));
	$row3 = $q->fetch();
}
?>
<?php if(!isset($_POST['action'])){ ?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Edison Technology Employee TimeClock</h2>
    <?php
	if(isset($_GET['msg'])){
	?>
    <h3 style="color:#F00; margin-bottom:0px;"><?php echo $_GET['msg']; ?></h3>
    <meta http-equiv='refresh' content='5;URL=timecard.php'>
    <?php }else{ ?>
    <form action="" method="post">
    	<h3 style="color:#000; margin-bottom:0px;">Your Edison Banner ID:</h3>
        (without the @ symbol)<br/><br/>
        <input type="password" name="bannerid" id="bannerid" style="width:300px;" value="" /><br/>
        <input type="hidden" name="action" value="step1" />
        <input type="submit" value="Go" />
    </form>
    <?php } ?>
</div>
<script>
$(document).ready(function() {
	document.getElementById('bannerid').focus();
});
</script>
<?php }elseif($_POST['action'] == "step1"){ ?>
<?php
if($row3['sAction'] == "Punch In"){
?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Hello, <?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <input type="hidden" name="action" value="punch_out" />
        <input type="hidden" name="bannerid" value="<?php echo $_POST['bannerid'] ?>" />
        <input type="submit" value=" PUNCH OUT " style="width:400px; font-size:20px;" />
    </form>
    <form action="" method="post">
        <input type="hidden" name="action" value="view_time" />
        <input type="hidden" name="bannerid" value="<?php echo $_POST['bannerid'] ?>" />
        <input type="submit" value=" VIEW TIMECARD " style="width:400px; font-size:20px;" />
    </form>
    <br/>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php }else{ ?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Hello, <?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <input type="hidden" name="action" value="punch_in" />
        <input type="hidden" name="bannerid" value="<?php echo $_POST['bannerid'] ?>" />
        <input type="submit" value=" PUNCH IN " style="width:400px; font-size:20px;" />
    </form>
    <form action="" method="post">
        <input type="hidden" name="action" value="manual_punch_in" />
        <input type="hidden" name="bannerid" value="<?php echo $_POST['bannerid'] ?>" />
        <input type="submit" value=" MANUAL PUNCH IN " style="width:400px; font-size:20px;" />
    </form>
    <form action="" method="post">
        <input type="hidden" name="action" value="view_time" />
        <input type="hidden" name="bannerid" value="<?php echo $_POST['bannerid'] ?>" />
        <input type="submit" value=" VIEW TIMECARD " style="width:400px; font-size:20px;" />
    </form>
    <br/>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php } ?>
<?php }elseif($_POST['action'] == "punch_in"){ ?>
<?php
$sql = "INSERT INTO `tbTimecard` VALUES(null, :empid, :date, :timestamp, :action)";
db::Query($sql, array(':empid'=>$row['iID'], ':date'=>date("n/j/Y"), ':timestamp'=>time(), ':action'=>'Punch In'));
?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Successful Punch-In For<br/><?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php }elseif($_POST['action'] == "manual_punch_in"){ ?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Successful Punch-In For<br/><?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <h3 style="color:#000; margin-bottom:0px;">Time In:</h3>
        <input type="password" name="bannerid" id="bannerid" style="width:300px;" value="" /><br/>
    </form>
</div>
<?php }elseif($_POST['action'] == "punch_in"){ ?>
<?php
$sql = "INSERT INTO `tbTimecard` VALUES(null, :empid, :date, :timestamp, :action)";
db::Query($sql, array(':empid'=>$row['iID'], ':date'=>date("n/j/Y"), ':timestamp'=>time(), ':action'=>'Punch In'));
?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Successful Punch-In For<br/><?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php }elseif($_POST['action'] == "view_time"){ ?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Time Card For<br/><?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <table width="500">
    	<tr>
        	<td>DATE</td>
            <td>IN</td>
            <td>OUT</td>
            <td>HRS</td>
            <td>SCH</td>
		</tr>
	<?php
	$sql = "SELECT COUNT(*) as `sCount` FROM `tbTimecard` WHERE `iTimeStamp` > :time AND `iEmployeeID` = :id";
	if(date("j") > 15){
		$q = db::Query($sql, array(':time'=>strtotime(date("n")."/16/".date("Y")), ':id'=>$row['iID']));
	}else{
		$q = db::Query($sql, array(':time'=>strtotime(date("n")."/1/".date("Y")), ':id'=>$row['iID']));
	}
	$count = $q->fetch();
	$sql = "SELECT * FROM `tbTimecard` WHERE `iTimeStamp` > :time AND `iEmployeeID` = :id";
	if(date("j") > 15){
		$q = db::Query($sql, array(':time'=>strtotime(date("n")."/16/".date("Y")), ':id'=>$row['iID']));
	}else{
		$q = db::Query($sql, array(':time'=>strtotime(date("n")."/1/".date("Y")), ':id'=>$row['iID']));
	}
	$lastDate = NULL;
	$lastIn = NULL;
	$inCount = 0;
	$total = 0;
	$dateTotal = 0;
	$ldate = NULL;
	while($time = $q->fetch()){
		$sql = "SELECT COUNT(*) as `sCount` FROM `tbTimecard` WHERE `sDate` = :date AND `iEmployeeID` = :id";
		$q2 = db::Query($sql, array(':date'=>$time['sDate'], ':id'=>$row['iID']));
		$date = $q2->fetch();
		$dateTotal ++;
		if($ldate == NULL){
			$ldate = $time['sDate'];
			$dateTotal = 1;
		}else{
			if($ldate != $time['sDate']){
				$ldate = NULL;
			}
		}
	?>
    	<?php $inCount ++; ?>
    	<?php if($lastIn != NULL or $inCount == $count['sCount']){ ?>
		<tr>
        	<?php if($lastDate != $time['sDate']){ ?>
            <?php $lastDate = $time['sDate'] ?>
            <td><?php echo $time['sDate'] ?></td>
            <?php
			$total = 0;
			?>
            <?php }else{ ?>
            <td>&nbsp;</td>
            <?php } ?>
            <?php if($inCount == $count['sCount']){ ?>
	            <?php if($lastIn == NULL){ ?>
	            <td><?php echo date("g:i:s A", $time['iTimeStamp']) ?></td>
                <td>&nbsp;</td>
                <?php }else{ ?>
            	<td><?php echo date("g:i:s A", $lastIn) ?></td>
	            <td><?php echo date("g:i:s A", $time['iTimeStamp']) ?></td>
                <?php } ?>
            <?php }else{ ?>
            	<td><?php echo date("g:i:s A", $lastIn) ?></td>
	            <td><?php echo date("g:i:s A", $time['iTimeStamp']) ?></td>
            <?php } ?>
            <?php if($inCount == $count['sCount'] or $date['sCount'] == $dateTotal){ ?>
            	<td>
				<?php
					if($lastIn == NULL){
						$dateDiff = time() - $time['iTimeStamp'];
					}else{
	                	$dateDiff = $time['iTimeStamp'] - $lastIn;
					}
					$hrs = ($dateDiff/60/60);
					$hrs = round($hrs, 2);
					if($hrs != 0){
						$total += $hrs;
					}
					echo number_format($total, 2);
				?>
                </td>
	            <td><?php echo $row['iHours'.date("D", $lastIn)] ?></td>
            <?php }else{ ?>
            	<td>
				<?php
                	$dateDiff = $time['iTimeStamp'] - $lastIn;
					$hrs = ($dateDiff/60/60);
					$hrs = round($hrs, 2);
					if($hrs != 0){
						$total += $hrs;
					}
				?>
                </td>
                <td>-</td>
            <?php } ?>
		</tr>
        <?php $lastIn = NULL; ?>
        <?php }else{ ?>
        <?php $lastIn = $time['iTimeStamp'] ?>
        <?php } ?>
    <?php } ?>
    </table>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php }elseif($_POST['action'] == "punch_out"){ ?>
<?php
$sql = "INSERT INTO `tbTimecard` VALUES(null, :empid, :date, :timestamp, :action)";
db::Query($sql, array(':empid'=>$row['iID'], ':date'=>date("n/j/Y"), ':timestamp'=>time(), ':action'=>'Punch Out'));
?>
<div id="content" align="center">
	<h2 style="color:#000; margin-top:-80px;">Successful Punch-Out For<br/><?php echo $row2['SPRIDEN_FIRST_NAME']." ".$row2['SPRIDEN_LAST_NAME'] ?></h2>
    <form action="" method="post">
        <input type="submit" value=" EXIT " style="width:400px; font-size:20px;" />
    </form>
</div>
<?php } ?>
</body>
</html>
